package com.qfjy.project.weixin.api.accessToken;

import com.qfjy.project.weixin.main.MenuManager;
import com.qfjy.project.weixin.pojo.AccessToken;
import com.qfjy.project.weixin.util.WeixinUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

/**
 * @author yr
 * @version 1.0
 * @date 2023/11/7 14:24
 */
@Slf4j
@Service
public class AccessTokenThread extends Thread{

    public static String WEIXIN_TOKEN_VALUE;
    @Override
    public void run(){
        while (true){
            WEIXIN_TOKEN_VALUE=getWeiXinAccessToken();
            log.info("获取的token为{}",WEIXIN_TOKEN_VALUE);
            try {
                Thread.sleep(1000*60*60);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    /**
     * 获取access_token
     *
     * @param appid 凭证
     * @param appsecret 密钥
     * @return
     */
    private static String ACCESS_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    public static String getWeiXinAccessToken() {

        String requestUrl = ACCESS_TOKEN_URL.replace("APPID", MenuManager.appId).replace("APPSECRET", MenuManager.appSecret);
        JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
        String access_token_val = jsonObject.getString("access_token");
        return access_token_val;

    }
}
